 /**
 * Description: 系统权限控制器
 * Copyright:Copyright 2018 nfky.com. All rights reserved
 * @author:xk
 * @since:0.0.1
 * Create at:2018-01-10 下午 19:43:58
 *  
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-01-10   xk       0.0.1     Initial
 */
package com.metro.paperless.sys.controller;

import com.github.pagehelper.PageInfo;
import com.metro.core.annotation.BusinessDesc;
import com.metro.core.excel.JxlsView;
import com.metro.core.page.Page;
import com.metro.core.vo.JsonResult;
import com.metro.core.vo.QueryParams;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.paperless.common.beans.constant.DicTypeEnum;
import com.metro.paperless.common.beans.constant.JxlsTemplateEnum;
import com.metro.paperless.common.utils.DicUtil;
import com.metro.paperless.sys.beans.vo.SysRoleMenu;
import com.metro.paperless.sys.beans.vo.ZTreeNode;
import com.metro.paperless.sys.service.inf.ISysRoleMenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;

import com.metro.paperless.sys.service.inf.ISysPermissionService;
import com.metro.paperless.sys.beans.vo.SysPermission;
import com.metro.paperless.web.controller.BaseController;

import java.util.List;

 /**
 * 系统权限控制器<br>
 *
 * @author xk
 * @since [0.0.1]
 * @version [0.0.1, 2018-01-10]
 */
@Controller
@RequestMapping("/syspermission")
public class SysPermissionController extends BaseController {
    //菜单编码
    private static final String menuCode = "syspermission";
    //模块描述
    private static final String MODULE_DESC = "系统权限";
    @Autowired
    private ISysPermissionService sysPermissionService;
    @Autowired
    private ISysRoleMenuService sysRoleMenuService;
    @Override
    protected String getViewDir() {
        return "sys/";
    }
    
    /**
     * 去查询系统权限
     * 
     * @param sysPermission 系统权限页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询系统权限列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "list", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView list(SysPermission sysPermission) {
        ModelAndView mav = new ModelAndView();
        try {
            mav.addObject("subsystemList", DicUtil.getLabelValueList(DicTypeEnum.SUB_SYSTEM));
            mav.addObject("permissionTypeList", DicUtil.getLabelValueList(DicTypeEnum.PERMISSION_TYPE));
            mav.addObject("dataStatusList", DicUtil.getLabelValueList(DicTypeEnum.DATA_STATUS));
            mav.setViewName(getView("syspermission_list"));
            mav.addObject("searchEntity", sysPermission);
        } catch (Exception ex) {
            log.error("查询系统权限出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }

    /**
     * 查询系统权限
     *
     * @param sysPermission 系统权限页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询系统权限列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "dolist", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JsonResult dolist(SysPermission sysPermission, Page page, QueryParams<SysPermission> queryParams) {
        JsonResult jr = new JsonResult();
        try {
            queryParams.setPaging(page);
            queryParams.setEntity(sysPermission);
            PageInfo<SysPermission> pageInfo = sysPermissionService.queryForPage(queryParams);
            jr.setData(pageInfo);
            jr.setResult(ResultCode.SUCCESS);
        } catch (Exception ex){
            log.error("查询系统权限出错",ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 去新增系统权限
     * 
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去新增系统权限")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "create")
    public ModelAndView createForm(Model model, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            initCreateOrUpdateFrom(mav, "create");
        } catch (Exception ex) {
            log.error("去新增系统权限出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }
    
    /**
     * 初始化新增修改系统权限所需参数
     * @author xk
     */
    private void initCreateOrUpdateFrom(ModelAndView mav, String action){
        mav.addObject("action", action);
        mav.setViewName(getView("syspermission_form"));
    }
    
    /**
     * 新增系统权限
     * 
     * @param entity 系统权限页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "新增系统权限")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "save", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult saveSysPermission(SysPermission entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = sysPermissionService.save(entity);
            jr.setResult(res);
            log.debug("新增系统权限成功【{}】", entity);
        } catch (Exception ex) {
            log.error("新增系统权限出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 删除系统权限
     * 
     * @param id 系统权限页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "删除系统权限")
    @RequiresPermissions(menuCode + ":delete")
    @RequestMapping(value = "delete/{id}", method = { RequestMethod.DELETE })
    @ResponseBody
    public JsonResult delete(@PathVariable Long id, HttpServletRequest request){
        JsonResult jr = new JsonResult();
        try {
            log.debug("删除系统权限id={}",id);
            IResult codeMsg = sysPermissionService.deletePermissionAndSubById(id);
            jr.setResult(codeMsg);
        } catch (Exception ex) {
            log.error("删除系统权限出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 去修改系统权限
     * 
     * @param id 系统权限页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去修改系统权限")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "edit/{id}", method = { RequestMethod.GET})
    public ModelAndView updateForm(@PathVariable Long id, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            SysPermission entity = sysPermissionService.getById(id);
            mav.addObject("entity", entity);
            initCreateOrUpdateFrom(mav,"edit");
        } catch (Exception ex) {
            log.error("去修改系统权限", ex);
            mav.setView(new RedirectView("list"));
        }
        return mav;
    }

    /**
     * 修改系统权限
     * 
     * @param entity 系统权限页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-10]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "修改系统权限")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "update", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult updateSysPermission(SysPermission entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = sysPermissionService.update(entity);
            jr.setResult(res);
            log.debug("修改系统权限成功【{}】", entity);
        } catch (Exception ex) {
            log.error("修改系统权限出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

     /**
      * 去查看系统权限
      *
      * @param id 系统权限页面表单对象唯一标识
      * @return 结果视图
      * @author xk
      * @since [0.0.2]
      * @version [0.0.2, 2016-06-14]
      */
     @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去查看系统权限详情")
     @RequiresPermissions(menuCode + ":view")
     @RequestMapping(value = "info/{id}", method = { RequestMethod.GET })
     @ResponseBody
     public JsonResult info(@PathVariable Long id) {
         JsonResult jr = new JsonResult();
         SysPermission entity = sysPermissionService.getById(id);
         QueryParams<SysRoleMenu> params = new QueryParams<>();
         SysRoleMenu roleMenu = new SysRoleMenu();
         roleMenu.setMenuId(id);
         params.setEntity(roleMenu);
         List<SysRoleMenu> roleMenuList = sysRoleMenuService.queryForList(params);
         entity.setRoleMenuList(roleMenuList);
         jr.setData(entity);
         jr.setResult(ResultCode.SUCCESS);
         return jr;

     }

     /**
      * 查询系统菜单的ztree树形结构
      */
     @RequestMapping(value = "queryPermissionTree", method = { RequestMethod.GET })
     @ResponseBody
     public JsonResult queryPermissionTree() {
         JsonResult jr = new JsonResult();
         List<ZTreeNode> list = sysPermissionService.queryPermissionTree();
         jr.setData(list);
         jr.setResult(ResultCode.SUCCESS);
         return jr;
     }

     @RequestMapping(value = "batchImportForm", method = RequestMethod.GET)
     public ModelAndView batchImportForm() {
         ModelAndView mav = new ModelAndView();
         mav.setViewName(getView("permission_import"));
         return mav;
     }

     /**
      * 批量导入
      * @return
      */
     @RequestMapping(value = "/batchImport")
     @ResponseBody
     public JsonResult batchImport(String filePath, HttpServletRequest request) {
         JsonResult jr = new JsonResult();
         try {
             jr = sysPermissionService.batchImport(filePath);
             log.debug("批量导入结果：{}", jr);
         } catch (Exception ex) {
             log.error("批量导入", ex);
             jr.setResult(ResultCode.ERROR);
         }
         return jr;
     }

     @RequestMapping(value = "export", method = RequestMethod.GET)
     public ModelAndView export() {
         SysPermission entity = null;
         List<SysPermission> list = sysPermissionService.queryForList(entity);
         return new ModelAndView(new JxlsView(JxlsTemplateEnum.PERMISSION, list));
     }
}
